package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公告 信息操作处理
 * 
 * @author ruoyi
 */
@Api("通知公告")
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysNoticeMapper sysNoticeMapper;

    /**
     * 获取通知公告列表
     */
    @ApiOperation("通知公告列表")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        if ("pc".equals(notice.getAppSource())) {
            List<SysNotice> list = noticeService.selectNoticeList(notice);
            fillInfo(list);
            return getDataTable(list);
        } else {
            notice.setReceiver(SecurityUtils.getUserId());
            List<SysNotice> list = sysNoticeMapper.selectMyNoticeListDesc(notice);
            fillInfo(list);
            return getDataTable(list);
        }
    }

    private void fillInfo(List<SysNotice> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<Long> userIds = list.stream()
                .filter(s -> s.getReceiver() != null)
                .map(SysNotice::getReceiver)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(userIds)) {
            return;
        }
        List<SysUser> sysUsers = sysUserMapper.selectUserByUserIds(userIds.toArray(new Long[0]));
        if (CollectionUtils.isEmpty(sysUsers)) {
            return;
        }
        Map<Long, SysUser> sysUserMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, s -> s));
        for (SysNotice notice : list) {
            if (notice.getReceiver() == null) {
                continue;
            }
            SysUser sysUser = sysUserMap.get(notice.getReceiver());
            if (sysUser != null) {
                notice.setReceiverName(sysUser.getNickName());
            }
        }
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    //("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    //("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        notice.setCreateBy(getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    //("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        notice.setUpdateBy(getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    //("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }
}
